/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.game.entity.hut;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.Image;
import towerz.game.level.Level;
import towerz.system.BitmapFont;
import towerz.system.StringManager;
import towerz.system.Utilities;
import towerz.system.xml.XmlNode;

/**
 *
 * @author LegACy
 */
public class HutData {
	protected HutData() {
		//Initialize
		m_Cost				= 0;
		m_Index				= 0;
		m_Range				= 0;
		m_RangeSquare		= -1;
		m_Sell				= Utilities.TOWER_SELL;
		m_Towers			= null;
		m_Image				= null;
		m_NameTable			= new Hashtable();
		m_DescriptionTable	= new Hashtable();
		m_Upgrades			= new int[0];
	}

	public HutData(XmlNode xml) {
		//Default
		this();

		//Default value for bonus-damage and bonus-speed
		m_BonusDamage = 0;
		m_BonusSpeed = 1.0f;

		//Get index
		m_Index = Integer.parseInt(xml.getAttribute("index"));

		//For each children
		for (int i = 0; i < xml.getChildren().size(); i++) {
			//Get values
			String Tag		= ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();
			String Value	= ((XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(0)).getNodeValue();

			//Based on tag name
			if (Tag.equals("cost"))					m_Cost = Integer.parseInt(Value);
			else if (Tag.equals("sell"))			m_Sell = Float.parseFloat(Value);
			else if (Tag.equals("limit"))			m_Limit = Integer.parseInt(Value);
			else if (Tag.equals("range"))			m_Range = Integer.parseInt(Value);
			else if (Tag.equals("range-square"))	m_RangeSquare = Integer.parseInt(Value);
			else if (Tag.equals("range"))			m_Range = Integer.parseInt(Value);
			else if (Tag.equals("bonus-damage"))	m_BonusDamage = Integer.parseInt(Value);
			else if (Tag.equals("bonus-speed"))		m_BonusSpeed = Float.parseFloat(Value);
			else if (Tag.equals("upgrades")) {
				//Get upgrades
				String[] Upgrades = Utilities.split(Value, ";");
				if (Upgrades.length > 0) m_Upgrades = new int[Upgrades.length];

				//For each hut
				for (int j = 0; j < m_Upgrades.length; j++)
					m_Upgrades[j] = Integer.parseInt(Upgrades[j]);
			} else if (Tag.equals("towers")) {
				//Get towers
				String[] Towers = Utilities.split(Value, ";");
				if (Towers.length > 0) m_Towers = new int[Towers.length];

				//For each tower
				for (int j = 0; j < Towers.length; j++)
					m_Towers[j] = Integer.parseInt(Towers[j]);
			} else if (Tag.equals("image")) {
				//Get image
				try {
					m_Image = Image.createImage(Utilities.HUT_FOLDER + "/" + Value + ".png");
				} catch (IOException ex) {}
			}
		}

		//Check range square
		if (m_RangeSquare < 0) m_RangeSquare = m_Range * m_Range;
	}
	
	public void loadDescription(XmlNode xml) {
		//Initialize
		int i				= 0;
		
		//For each children
		while (i < xml.getChildren().size()) {
			//Get values
			String Tag		= ((XmlNode)xml.getChildren().elementAt(i)).getNodeName();

			//Based on tag name
			if( Tag.equals("names")) {
				//Init
				int j = 0;
				
				//For each child
				while (j < ((XmlNode)xml.getChildren().elementAt(i)).getChildren().size()) {
					//Get child
					XmlNode Child = (XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(j);
					//If name
					if (Child.getNodeName().equals("name"))
						m_NameTable.put(Child.getAttribute("language"), BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((XmlNode)Child.getChildren().elementAt(0)).getNodeValue(), 0x00000000));
					
					//Next
					j++;
				}
			}
			else if (Tag.equals("descriptions")) {
				//Init
				int j = 0;
				
				//For each child
				while (j < ((XmlNode)xml.getChildren().elementAt(i)).getChildren().size()) {
					//Get child
					XmlNode Child = (XmlNode)((XmlNode)xml.getChildren().elementAt(i)).getChildren().elementAt(j);
					
					//If name
					if (Child.getNodeName().equals("description"))
						m_DescriptionTable.put(Child.getAttribute("language"), BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((XmlNode)Child.getChildren().elementAt(0)).getNodeValue(), 0x00000000));
					
					//Next
					j++;
				}
			}
			
			//Next 
			i++;
		}
	}

	//Accessors
	public int getCost()			{	return m_Cost;					}
	public int getSell()			{	return (int) (m_Cost * m_Sell);	}
	public int getLimit()			{	return m_Limit;					}
	public int getIndex()			{	return m_Index;					}
	public int getRange()			{	return m_Range;					}
	public Image getImage()			{	return m_Image;					}
	public int[] getUpgrades()		{	return m_Upgrades;				}
	public int getRangeSquare()		{	return m_RangeSquare;			}
	public int getBonusDmg()		{	return m_BonusDamage;			}
	public float getBonusSpd()		{	return m_BonusSpeed;			}
	
	public Image getNameString() {
		return (Image)m_NameTable.get(StringManager.instance().getTag());
	}
	
	public Image getDescString() {
		return (Image)m_DescriptionTable.get(StringManager.instance().getTag());
	}

	//Get available towers
	public int[] getTowers(Level level) {
		//Variable
		Vector Towers	= new Vector();
		int[] Available = level.getAvailableTowers();

		//For each tower
		for (int i = 0; i < m_Towers.length; i++) {
			//Check whether is it available or no
			for (int j = 0; j < Available.length; j++)
				if (m_Towers[i] == Available[j]) Towers.addElement(new Integer(m_Towers[i]));
		}

		//Convert to array
		int[] TowerArray = new int[Towers.size()];
		for (int i = 0; i < TowerArray.length; i++) TowerArray[i] = ((Integer)Towers.elementAt(i)).intValue();

		//Return the array
		return TowerArray;
	}

	//Get available huts
	public int[] getAvailableUpgrades (Level level) {
		//Variables
		Vector Huts		= new Vector();
		int[] Available	= level.getAvailableHuts();

		for (int i = 0; i < Available.length; i++) {
			for (int j = 0; j < m_Upgrades.length; j++) {
				if (Available[i] == m_Upgrades[j]) Huts.addElement(new Integer(Available[i]));
			}
		}

		//COnvert to array
		int[] HutArray = new int[Huts.size()];
		for (int i = 0; i < HutArray.length; i++) HutArray[i] = ((Integer)Huts.elementAt(i)).intValue();

		//Return
		return HutArray;
	}

	//Data
	protected int		m_Cost;
	protected int		m_Limit;
	protected int		m_Index;
	protected int		m_Range;
	protected int		m_RangeSquare;
	protected float		m_Sell;
	protected Image		m_Image;
	protected int[]		m_Towers;
	protected int[]		m_Upgrades;

	//Bonus upgrade
	protected int		m_BonusDamage;
	protected float		m_BonusSpeed;
	
	//Images
	protected Hashtable m_NameTable;
	protected Hashtable m_DescriptionTable;
}
